package com.graduation.sfstore.controller;

import com.graduation.sfstore.dao.StoreMapper;
import com.graduation.sfstore.entity.*;
import com.graduation.sfstore.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private StoreService storeService;

    @Value("${server.servlet.context-path}")
    private String contextPath;

    @RequestMapping(path = "/consumerlogin", method = RequestMethod.GET)
    public String getLoginPage() {
        return "/site/login";
    }



    @RequestMapping(path = "/consumerlogin2", method = RequestMethod.POST)
    public String login(String name, String password, HttpServletResponse response) {

        Consumeruser consumeruser = storeService.findConsumer(name, password);
        Cookie cookie = new Cookie("consumeruser", String.valueOf(consumeruser.getId()));
        cookie.setMaxAge(600);
        cookie.setPath(contextPath);
        response.addCookie(cookie);
        if (consumeruser==null){
            return "505";
        }else {
            return "redirect:/shop";
        }

    }



    @RequestMapping(path="/shop",method = RequestMethod.GET)
    public String getShopPage(Model model, Page page){
        page.setRows(storeService.findSpeciesSortRows());
        page.setPath("/shop");

        List<SpeicesSort> list = storeService.findStoresSort(page.getOffset(),page.getLimit());
        List<Map<String, Object>> speciesSorts = new ArrayList<Map<String, Object>>();
        if (list!=null){
            for (SpeicesSort speicesSort:list) {
                System.out.println(speicesSort);
                Map<String, Object> map = new HashMap<>();
                map.put("speciesSort",speicesSort);
                speciesSorts.add(map);
            }
        }

        model.addAttribute("speciesSorts",speciesSorts);
        return "/shopping";
    }


    @RequestMapping(path="/buy",method = RequestMethod.GET)
    public String shopBuy(String name,String weight,String address,@CookieValue("consumeruser") String consumeruser){

        SpeicesSort speicesSort = storeService.findStores(name);

        OrderForm orderForm = new OrderForm();
        orderForm.setConsumerid(Integer.parseInt(consumeruser));
        orderForm.setName(name);
        orderForm.setAddress(address);
        orderForm.setStatus(0);
        orderForm.setWeight(Double.parseDouble(weight));
        orderForm.setRegion(speicesSort.getRegion());
        storeService.insertOrder(orderForm);

        return "/shopping";
    }


}
